package cn.itsource.ymcc.service.impl;

import cn.itsource.ymcc.domain.Course;
import cn.itsource.ymcc.domain.CourseChapter;
import cn.itsource.ymcc.mapper.CourseChapterMapper;
import cn.itsource.ymcc.mapper.CourseMapper;
import cn.itsource.ymcc.service.ICourseChapterService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 课程章节 ， 一个课程，多个章节，一个章节，多个视频 服务实现类
 * </p>
 *
 * @author cff
 * @since 2024-06-11
 */
@Service
public class CourseChapterServiceImpl extends ServiceImpl<CourseChapterMapper, CourseChapter> implements ICourseChapterService {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CourseChapterMapper courseChapterMapper;


    @Transactional
    @Override
    public void saveCourseChapter(CourseChapter courseChapter) {
        Course course = courseMapper.selectById(courseChapter.getCourseId());
        course.setChapterCount(course.getChapterCount() + 1);
        courseMapper.updateById(course);
        List<CourseChapter> courseChapters = courseChapterMapper.selectList
                (new LambdaQueryWrapper<CourseChapter>()
                .eq(CourseChapter::getCourseId, courseChapter.getCourseId()));
        int length = courseChapters.size();
        if (length == 0){
            courseChapterMapper.insert(courseChapter);
        }else {
            courseChapter.setNumber(length + 1);
            courseChapterMapper.insert(courseChapter);
        }
    }

    @Transactional
    @Override
    public void removeCourseChapterById(Long id) {
        CourseChapter courseChapter = courseChapterMapper.selectById(id);
        Course course = courseMapper.selectById(courseChapter.getCourseId());
        course.setChapterCount(course.getChapterCount() - 1);
        courseMapper.updateById(course);
        courseChapterMapper.deleteById(id);
    }

    @Override
    public void updateCourseChapter(CourseChapter courseChapter) {
        CourseChapter coursecp = courseChapterMapper.selectById(courseChapter.getId());
        Course course = courseMapper.selectById(coursecp.getCourseId());
        course.setChapterCount(course.getChapterCount() - 1);
        courseMapper.updateById(course);
        super.removeById(courseChapter.getId());

        List<CourseChapter> courseChapters = courseChapterMapper.selectList
                (new LambdaQueryWrapper<CourseChapter>()
                        .eq(CourseChapter::getCourseId, courseChapter.getCourseId()));
        int length = courseChapters.size();
        if (length == 0){
            courseChapterMapper.insert(courseChapter);
        }else {
            courseChapter.setNumber(length + 1);
            courseChapterMapper.insert(courseChapter);
        }
        Course upCourse = courseMapper.selectById(courseChapter.getCourseId());
        course.setChapterCount(upCourse.getChapterCount() + 1);
        courseMapper.updateById(upCourse);
    }
}
